# -*- encoding:utf-8 -*-
from mongokit import Document,Connection
import datetime
import json

class Survey(Document):
    structure = {
            'created':datetime.datetime,
            'account':unicode,
            'results':dict,
    }
    required_fields = ['created','account',]
    use_dot_notation = True

con = Connection()
con.register([Survey])



from sqlalchemy import create_engine,MetaData,Table

DB_URI = 'mysql://dagger:jH83dap9@125.77.200.20:9527/dagger3?charset=utf8'
engine = create_engine(DB_URI,echo=True)
connection = engine.connect()
try:
    last_created = con.dagger.survey.find().sort('created',-1).next()['created']
    results = engine.execute("select `created`,`customer_account`,`data` from customer_accounts where `created` > '%s' limit 1000000"%last_created).fetchall()
except:
    results = engine.execute("select `created`,`customer_account`,`data` from customer_accounts limit 100").fetchall()
for result in results:
    if 'survey201006' not in result['data']:
        continue
    data = json.loads(result['data'])
    survey = con.dagger.survey.Survey()
    survey.created = result['created']
    survey.account = result['customer_account']
    survey.results = {} if data['survey201006'] == 'no' else data['survey201006'] 
    survey.save()
